package database.dao.Impl;


import database.dao.CommodityDao;
import entity.Commodity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CommodityDaoImpl
 * @Description TODO
 * @Author lzq
 * @Date 2021/6/17 8:53
 */
public class CommodityDaoImpl extends BaseDao implements CommodityDao {

    @Override
    public void addCommodity(Commodity commodity) {
        String psql="insert into Commodity(name,picture,price,`describe`,quantity,business_id,commodity_type)" +
                "values(?,?,?,?,?,?,?)";
        Object[] values={commodity.getName(),commodity.getPicture(),commodity.getPrice(),commodity.getDescribe(),commodity.getQuantity(),commodity.getBusiness_id(),commodity.getCommodity_type()};
        super.executeUpdate(psql,values);
    }

    @Override
    public List<Commodity> SelectCommodity(String business_id) {
        ResultSet resultSet = null;
        String psql="SELECT * FROM Commodity WHERE business_id= ?";
        Object[] value= new Object[]{business_id};
        resultSet= super.executeQuerySQL(psql,value);
        return getCommodityList(resultSet);
    }

    @Override
    public List<Commodity> SelectCommodity(String business_id, String name) {
        String psql="SELECT * FROM Commodity WHERE name like ?";
        ResultSet resultSet = null;
        Object[] value= new Object[]{("%"+name+"%")};
        if(business_id!=null){
            psql+=" and business_id= ? ";
            value=new Object[]{("%"+name+"%"),business_id};
        }
        resultSet= super.executeQuerySQL(psql,value);
        return getCommodityList(resultSet);
    }

    @Override
    public List<Commodity> SelectCommodity() {
        ResultSet resultSet = null;
        String psql="SELECT * FROM Commodity";
        Object[] value= new Object[]{};
        resultSet= super.executeQuerySQL(psql,value);
        return getCommodityList(resultSet);
    }


    public List<Commodity> getCommodityList(ResultSet resultSet) {
        List<Commodity>commodities=new ArrayList<>();
        if(resultSet!=null){
            try{
                while(resultSet.next()){
                    String name=resultSet.getString("name");
                    Float price=resultSet.getFloat("price");
                    Integer quantity=resultSet.getInt("quantity");
                    String commodity_type=resultSet.getString("commodity_type");
                    String picture=resultSet.getString("picture");
                    String describe=resultSet.getString("describe");
                    Integer commodity_id=resultSet.getInt("commodity_id");
                    String business_id=((Integer) resultSet.getInt("business_id")).toString();
                    commodities.add(new Commodity(commodity_id,name,price,quantity,commodity_type,picture,describe, business_id));
                }
            }catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return commodities;
    }

    @Override
    public void deleteCommodity(String commodity_id) {
        String psql="delete from Commodity WHERE commodity_id= ? ";
        Object[] value={ commodity_id };
        super.executeUpdate(psql,value);
    }

    @Override
    public void updateCommodity(Commodity commodity) {
        String psql="update  Commodity set name = ?,price = ?,`describe` = ?,quantity = ? where commodity_id=?";
        Object[] values={commodity.getName(),commodity.getPrice(),commodity.getDescribe(),commodity.getQuantity(),commodity.getCommodity_id()};
        super.executeUpdate(psql,values);
    }

}
